maintainer="Hoang Nguyen <folliekazetani@protonmail.com>"
pkgname=buf
pkgver=1.45.0
pkgrel=0
pkgdesc="CLI to work with Protocol Buffers"
url="https://buf.build/"
# 32bit: fail tests with int overflow
arch="all !x86 !armhf !armv7"
license="Apache-2.0"
makedepends="go"
checkdepends="git-daemon protobuf-dev protoc-gen-go"
subpackages="
	$pkgname-bash-completion
	$pkgname-fish-completion
	$pkgname-zsh-completion
	$pkgname-protoc-plugins:_protoc
	"
source="$pkgname-$pkgver.tar.gz::https://github.com/bufbuild/buf/archive/refs/tags/v$pkgver.tar.gz
	remove-deprecated-testcorpus-field.patch
	"
options="net" # download Go modules

# riscv64, ppc64le: private/bufpkg/bufstudioagent tests time out
# s390x: tests making GET requests to external HTTP endpoints easily (context deadline exceeded)
case "$CARCH" in
	riscv64|ppc64le|s390x) options="$options !check"
esac

export GOCACHE="${GOCACHE:-"$srcdir/go-cache"}"
export GOTMPDIR="${GOTMPDIR:-"$srcdir"}"
export GOMODCACHE="${GOMODCACHE:-"$srcdir/go"}"

build() {
	for binary in buf protoc-gen-buf-breaking protoc-gen-buf-lint; do
		go build -v ./cmd/$binary
	done

	for shell in bash fish zsh; do
		./buf completion $shell > buf.$shell
	done

	# These programs are required for check phase
	# See https://github.com/bufbuild/buf/blob/main/make/buf/all.mk for the list of binaries needed to be built
	mkdir -p "$builddir"/bin
	go build -v -o bin/ ./private/buf/cmd/buf/command/generate/internal/protoc-gen-top-level-type-names-yaml
	for cmd in writer receiver; do
		go build -v -o bin/ ./private/buf/cmd/buf/command/alpha/protoc/internal/protoc-gen-insertion-point-$cmd
	done
	for plugin in panic suffix protovalidate-ext rpc-ext duplicate-category duplicate-rule; do
		go build -v -o bin/ ./private/bufpkg/bufcheck/internal/cmd/buf-plugin-$plugin
	done
}

check() {
	export PATH="$PATH:$builddir/bin"

	# TestCompareGeneratedStubsGoogleapisGo fails due to difference in protoc versions
	# TestCompareGoogleapis fails (TODO: investigate more)
	# TestWorkspaceGit checks the project root whether it is a git repo
	# TestRunLintCustomWasmPlugins relies on buf-plugin-suffix.wasm file we can't produce with -buildmode=pie
	go test -skip 'TestCompareGeneratedStubsGoogleapisGo|TestCompareGoogleapis|TestWorkspaceGit|TestRunLintCustomWasmPlugins' ./...
}

package() {
	install -Dm755 buf -t "$pkgdir"/usr/bin/

	install -Dm644 buf.bash \
		"$pkgdir"/usr/share/bash-completion/completions/buf
	install -Dm644 buf.fish \
		"$pkgdir"/usr/share/fish/vendor_completions.d/buf.fish
	install -Dm644 buf.zsh \
		"$pkgdir"/usr/share/zsh/site-functions/_buf

	install -Dm755 protoc-gen-buf-breaking protoc-gen-buf-lint \
		-t "$pkgdir"/usr/bin/
}

_protoc() {
	pkgdesc="$pkgdesc (protoc plugins)"
	depends="protoc"

	amove usr/bin/protoc-gen-buf-*
}

sha512sums="
3783e79a971ba1a8b319095c722096b93c8b6fc327e0b489c772ba18d3be8e1d8aa493db4eff6eceaabde8ef7025649d61bdd5c777efd17b3cc54fb8dddf42a9  buf-1.45.0.tar.gz
0fa6fef80d2eba7d59344c458ecf5ddaa448dee338c3bc4d24d4a29748eedd98ab2046847ddbf96e09f8c99b7c0dcb1ecb69389e344c042169c838a6a91b8ea9  remove-deprecated-testcorpus-field.patch
"
